package com.web.treatment;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.domain.Treatments;
import com.service.TreatmentsService;
import com.util.AuthenticateHttpServlet;

/**
 * Servlet implementation class DropDownProductList
 */

@WebServlet("/Treatment/DropDownTreatmentList")
public class DropDownTreatmentList extends AuthenticateHttpServlet {
	private static final long serialVersionUID = 1L;

	protected void post(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		TreatmentsService service = new TreatmentsService();
		List<Treatments> list = service.getAllTreatment();
		String xml = xmlProductList(list);
		PrintWriter w = response.getWriter();
		w.println(xml);
		w.close();
	}

	private String xmlProductList(List<Treatments> list) {
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("treatments");
		for (Treatments t : list) {
			Element treatmentNode = root.addElement("treatment");
			Element nameNode = treatmentNode.addElement("treatmentName");
			Element codeNode = treatmentNode.addElement("treatmentId");
			nameNode.setText(t.getTreatmentName());
			codeNode.setText(t.getTreatmentCode());
		}
		return root.asXML();
	}
}
